home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Freeware / Griffith 0.9.8 / griffith-0.9.8-win32.exe / {app} / lib / plugins / movie / PluginMovieIMDB.py < prev    next >
Text File  |  2008-11-17  |  8KB  |  223 lines

  1. # -*- coding: UTF-8 -*-
  2.  
  3. __revision__ = '$Id: PluginMovieIMDB.py 1040 2008-11-15 21:13:49Z mikej06 $'
  4.  
  5. # Copyright (c) 2005-2007 Vasco Nunes, Piotr O┼╝arowski
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Library General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  20.  
  21. # You may use and distribute this software under the terms of the
  22. # GNU General Public License, version 2 or later
  23.  
  24. import gutils, movie
  25. import string, re
  26.  
  27. plugin_name        = 'IMDb'
  28. plugin_description    = 'Internet Movie Database'
  29. plugin_url        = 'www.imdb.com'
  30. plugin_language        = _('English')
  31. plugin_author        = 'Vasco Nunes, Piotr O┼╝arowski'
  32. plugin_author_email    = 'griffith-private@lists.berlios.de'
  33. plugin_version        = '1.5'
  34.  
  35. class Plugin(movie.Movie):
  36.     def __init__(self, id):
  37.         self.encode = 'utf-8'
  38.         self.movie_id = id
  39.         self.url = "http://imdb.com/title/tt%s" % str(self.movie_id)
  40.  
  41.     def initialize(self):
  42.         self.cast_page = self.open_page(url=self.url + '/fullcredits')
  43.         self.plot_page = self.open_page(url=self.url + '/plotsummary')
  44.  
  45.     def get_image(self):
  46.         tmp = string.find(self.page, 'a name="poster"')
  47.         if tmp == -1:        # poster not available
  48.             self.image_url = ''
  49.         else:
  50.             self.image_url = gutils.trim(self.page[tmp:], 'src="', '"')
  51.  
  52.     def get_o_title(self):
  53.         self.o_title = self.regextrim(self.page, '<h1>', '([ ]|[&][#][0-9]+[;])<span')
  54.         if self.o_title == '':
  55.             self.o_title = re.sub('[(].*', '', gutils.trim(self.page, '<title>', '</title>'))
  56.  
  57.     def get_title(self):    # same as get_o_title()
  58.         self.title = self.regextrim(self.page, '<h1>', '([ ]|[&][#][0-9]+[;])<span')
  59.         if self.title == '':
  60.             self.title = re.sub('[(].*', '', gutils.trim(self.page, '<title>', '</title>'))
  61.  
  62.     def get_director(self):
  63.         pattern = re.compile('<h5>Director[s]*?:</h5>[\n\s\r]*(.*?)(?:<br/>)?(?:<a[^>]+>more</a>)?[\n]*</div')
  64.         result = pattern.search(self.page)
  65.         if result:
  66.             self.director = result.groups()[0]
  67.             self.director = self.director.replace('<br/>', ', ')
  68.  
  69.     def get_plot(self):
  70.         self.plot = gutils.trim(self.page, '<h5>Plot Outline:</h5>', '</div>')
  71.         self.plot = self.__before_more(self.plot)
  72.         elements = string.split(self.plot_page, '<p class="plotpar">')
  73.         if len(elements) > 1:
  74.             self.plot = self.plot + '\n\n'
  75.             elements[0] = ''
  76.             for element in elements:
  77.                 if element <> '':
  78.                     self.plot = self.plot + gutils.strip_tags(gutils.before(element, '</a>')) + '\n'
  79.  
  80.     def get_year(self):
  81.         self.year = gutils.trim(self.page, '<a href="/Sections/Years/', '</a>')
  82.         self.year = gutils.after(self.year, '">')
  83.  
  84.     def get_runtime(self):
  85.         self.runtime = gutils.trim(self.page, '<h5>Runtime:</h5>', ' min')
  86.  
  87.     def get_genre(self):
  88.         self.genre = gutils.trim(self.page, '<h5>Genre:</h5>', '</div>')
  89.         self.genre = self.__before_more(self.genre)
  90.  
  91.     def get_cast(self):
  92.         self.cast = ''
  93.         self.cast = gutils.trim(self.cast_page, '<table class="cast">', '</table>')
  94.         if self.cast == '':
  95.             self.cast = gutils.trim(self.page, '<table class="cast">', '</table>')
  96.         self.cast = string.replace(self.cast, ' ... ', _(' as '))
  97.         self.cast = string.replace(self.cast, '...', _(' as '))
  98.         self.cast = string.replace(self.cast, '</tr><tr>', "\n")
  99.         self.cast = string.replace(self.cast, '</tr><tr class="even">', "\n")
  100.         self.cast = string.replace(self.cast, '</tr><tr class="odd">', "\n")
  101.         self.cast = self.__before_more(self.cast)
  102.  
  103.     def get_classification(self):
  104.         self.classification = gutils.trim(self.page, '<h5><a href="/mpaa">MPAA</a>:</h5>', '</div>')
  105.         self.classification = gutils.trim(self.classification, 'Rated ', ' ')
  106.  
  107.     def get_studio(self):
  108.         self.studio = gutils.trim(self.page, '<h5>Company:</h5>', '</a>')
  109.  
  110.     def get_o_site(self):
  111.         self.o_site = ''
  112.  
  113.     def get_site(self):
  114.         self.site = "http://www.imdb.com/title/tt%s" % self.movie_id
  115.  
  116.     def get_trailer(self):
  117.         self.trailer = "http://www.imdb.com/title/tt%s/trailers" % self.movie_id
  118.  
  119.     def get_country(self):
  120.         self.country = gutils.trim(self.page, '<h5>Country:</h5>', '</div>')
  121.  
  122.     def get_rating(self):
  123.         pattern = re.compile('>([0-9]([.][0-9])*)[/][0-9][0-9]<')
  124.         result = pattern.search(self.page)
  125.         if result:
  126.             self.rating = result.groups()[0]
  127.             if self.rating:
  128.                 try:
  129.                     self.rating = float(self.rating)
  130.                 except Exception, e:
  131.                     self.rating = 0
  132.         else:
  133.             self.rating = 0
  134.  
  135.     def get_notes(self):
  136.         self.notes = ''
  137.         language = gutils.trim(self.page, '<h5>Language:</h5>', '</div>')
  138.         language = gutils.strip_tags(language)
  139.         language = re.sub('[\n]+', '', language)
  140.         language = re.sub('[ ]+', ' ', language)
  141.         language = language.rstrip()
  142.         color = gutils.trim(self.page, '<h5>Color:</h5>', '</div>')
  143.         color = gutils.strip_tags(color)
  144.         color = re.sub('[\n]+', '', color)
  145.         color = re.sub('[ ]+', ' ', color)
  146.         color = color.rstrip()
  147.         sound = gutils.trim(self.page, '<h5>Sound Mix:</h5>', '</div>')
  148.         sound = gutils.strip_tags(sound)
  149.         sound = re.sub('[\n]+', '', sound)
  150.         sound = re.sub('[ ]+', ' ', sound)
  151.         sound = sound.rstrip()
  152.         tagline = gutils.trim(self.page, '<h5>Tagline:</h5>', '</div>')
  153.         tagline = self.__before_more(tagline)
  154.         tagline = gutils.strip_tags(tagline)
  155.         tagline = re.sub('[\n]+', '', tagline)
  156.         tagline = re.sub('[ ]+', ' ', tagline)
  157.         tagline = tagline.rstrip()
  158.         if len(language)>0:
  159.             self.notes = "%s: %s\n" %(_('Language'), language)
  160.         if len(sound)>0:
  161.             self.notes += "%s: %s\n" %(gutils.strip_tags(_('<b>Audio</b>')), sound)
  162.         if len(color)>0:
  163.             self.notes += "%s: %s\n" %(_('Color'), color)
  164.         if len(tagline)>0:
  165.             self.notes += "%s: %s\n" %('Tagline', tagline)
  166.     
  167.     def __before_more(self, data):
  168.         tmp = string.find(data, '>more<')
  169.         if tmp>0:
  170.             data = data[:tmp] + '>'
  171.         return data
  172.  
  173.     def regextrim(self,text,key1,key2):
  174.         obj = re.search(key1, text)
  175.         if obj is None:
  176.             return ''
  177.         else:
  178.             p1 = obj.end()
  179.         obj = re.search(key2, text[p1:])
  180.         if obj is None:
  181.             return ''
  182.         else:
  183.             p2 = p1 + obj.start()
  184.         return text[p1:p2]
  185.  
  186. class SearchPlugin(movie.SearchMovie):
  187.     PATTERN = re.compile(r"""<A HREF=['"]/title/tt([0-9]+)/["']>(.*?)</LI>""")
  188.     PATTERN2 = re.compile(r"""<a href=['"]/title/tt([0-9]+)/["']>(.*?)</td>""")
  189.  
  190.     def __init__(self):
  191.         self.original_url_search    = 'http://www.imdb.com/List?words='
  192.         self.translated_url_search    = 'http://www.imdb.com/find?more=tt;q='
  193.         self.encode = 'utf-8'
  194.  
  195.     def search(self,parent_window):
  196.         self.open_search(parent_window)
  197.         tmp_page = gutils.trim(self.page, 'Here are the', '</TABLE>')
  198.         if tmp_page == '':
  199.             self.page = gutils.trim(self.page, '(Displaying', '<b>Suggestions For Improving Your Results</b>')
  200.         else:
  201.             self.page = tmp_page
  202.         self.page = self.page.decode('iso-8859-1')
  203.         return self.page
  204.  
  205.     def get_searches(self):
  206.         elements = re.split('<LI>', self.page)
  207.         if len(elements) < 2:
  208.             elements = string.split(self.page, '<tr>')
  209.             if len(elements):
  210.                 for element in elements[1:]:
  211.                     match = self.PATTERN2.findall(element)
  212.                     if len(match):
  213.                         tmp  = gutils.clean(match[0][1])
  214.                         self.ids.append(match[0][0])
  215.                         self.titles.append(tmp)
  216.         else:
  217.             for element in elements[1:]:
  218.                 match = self.PATTERN.findall(element)
  219.                 if len(match):
  220.                     tmp  = gutils.clean(match[0][1])
  221.                     self.ids.append(match[0][0])
  222.                     self.titles.append(tmp)
  223.